<?php
declare(strict_types=1);

namespace App\Controller;

use App\Model\data\CurrentShopData;
use App\Model\data\CurrentUserData;
use App\Service\TalkRoomService;
use Hyperf\Validation\Rule;
use Hyperf\Di\Annotation\Inject;

class UserTalkRoomController extends  AbstractController
{

    /**
     * @inject
     * @var TalkRoomService
     */
    private $talkRoom;

//    打开对话框
    public function get_room()
    {
        $shop_id = $this->request->input('shop_id');
        $this->validation(
            ['shop_id'=>$shop_id],
            [
                'shop_id'=>[
                    'required',
                    'integer',
                    'min:1',
                    Rule::exists('fly_shop','id')
                ]
            ],
        );
        $room_info  = $this->talkRoom->get_room(intval($shop_id),intval($this->getAdminId()));
        return $this->jsonResponse->success($room_info,'房间信息');
    }

//    todo  获取全部房间列表
    public function room_list()
    {
        $user_info = CurrentUserData::getData();
        $room_list = $this->paginate($this->talkRoom->room_list(intval($user_info->id),2))->toArray();
        foreach ($room_list['data'] as $key=>$value){
            $room_list['data'][$key]['un_read'] =  $this->talkRoom->count_size(intval($user_info->id),$value['id'],2,0);;
        }

        return $this->jsonResponse->success($room_list,'房间信息');
    }

    public function get_talk_log()
    {
        $params = $this->request->inputs(['room_id','type','user_id','shop_id']);
        $user_id = $this->getAdminId();
        $params['user_id'] = $user_id;
        $this->validation(
            $params,
            [
                'room_id'=>[
                    'required',
                    'integer',
                    'min:1',
                    Rule::exists('fly_shop','id')
                ],
                'user_id'=>[
                    'required',
                    'integer',
                    'min:1',
                    Rule::exists('fly_room_user','user_id')->where('type',$params['type'])
                ],
            ],[
                'user_id.exists'=>'用户不存在此会话中！'
            ]
            );
        $room_info =  $this->paginate($this->talkRoom->get_room_log(intval($params['room_id']),intval($user_id)));
        return $this->jsonResponse->success($room_info,'房间聊天记录');
    }
}
